<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="author" content="Wangrui">
    <script src="jquery-3.1.1.js"></script>
    <title>弹窗</title>
    <style>
        #content{
            height: 1500px;
            width: 2000px;
            background-color:azure;
        }
        #overlay{
            height: 100%;
            width: 100%;
            background-color: rgba(0, 0, 0, 0.1);
            position: absolute;
            top: 0;
            left: 0;
            z-index: 999;
            display: none;
        }
        #alert{
            width: 400px;
            height: 200px;
            background-color: #fff;
            position: absolute;
            top: 100px;
            left: 100px;
            z-index: 1000;
            display: none;
        }
        #alert h2{
            margin: 0;
            height: 40px;
            line-height: 40px;
            text-align: center;
            font-size: 16px;
            background-color: #eee;
            cursor: move;
            /*禁止用户选择页面上的内容*/
            user-select: none;
            -webkit-user-select: none;
            -moz-user-select: none;
            -ms-user-select: none;
            position: relative;
        }
        #alert h2 .close{
            display: inline-block;
            height: 30px;
            width: 30px;
            position: absolute;
            top: 5px;
            right: 5px;
            line-height: 30px;
            text-align: center;
            transition: all 1s linear;
        }
        #alert h2 .close:hover{
            cursor: pointer;
            background-color: #ccc;
        }
        #alert form{
            width: 100%;
            height: 160px;
            padding: 20px;
        }
        #alert div{
            line-height: 40px;
            text-align: center;
        }
        #alert input{
            width: 260px;
            height: 30px;
            padding-left: 10px;
        }
        #alert .login{
            border: none;
            font-size: 16px;
        }
        *{
            box-sizing: border-box;
        }
    </style>
</head>
<body>
    <!--内容区-->
    <div id="content"></div>
    <center><button type="button">来点我</button></center>
    <!--遮罩层-->
    <div id="overlay"></div>
    <!--弹窗-->
    <div id="alert">
        <h2>
            <span>用户登录</span>
            <span class="close">&times;</span>
        </h2>
        <form action="javascript:;">
            <div><label>账号：<input type="text" placeholder="请输入账号"></label></div>
            <div><label>密码：<input type="password" placeholder="请输入密码"></label></div>
            <div><input class="login" type="submit" value="登录"></div>
        </form>
    </div>
</body>
</html>
<script>
$(function(){
    // console.log($('#alert').width()+' '+$('#alert').innerWidth()+' '+$('#alert').outerWidth());

    // 遮罩层的大小永远都是100%
    setOverlayWH();
    function setOverlayWH(){
        // 整个页面的大小 可视页面的大小+滚动条的大小
        // var w = $(document).width();
        // var h = $(document).height();
        // console.log('w='+w+' h='+h);

        var w = $(window).width() + $(document).scrollLeft();
        var h = $(window).height() + $(document).scrollTop();
        // console.log('w='+w+' h='+h);
        $('#overlay').width(w).height(h);
    }
    // 弹窗第一次出现居中，当浏览器窗口发生变化时也要居中
    setAlertCenter();
    function setAlertCenter(){
        // top = (可视页面的高 - 弹窗的高)/2 + 滚动条的高
        var left = ($(window).width()-$('#alert').width())/2 + $(document).scrollLeft();
        var top = ($(window).height()-$('#alert').height())/2 + $(document).scrollTop();

        $('#alert').css({left:left,top:top});
    }
    // 当页面尺寸发生变化或者发生滚动时，重新计算尺寸
    $(window).resize(function(){
        setOverlayWH();
        setAlertCenter();
    });
    $(window).scroll(function(){
        setOverlayWH();
        setAlertCenter();
    });

    // 弹出
    $('button').click(function(){
        $('#overlay').show();
        $('#alert').show();
        // 禁用滚动条
        $('html').css('overflow','hidden');
        $('body').css('overflow','hidden');
    });
    // 关闭
    $('#alert .close').click(function(){
        $('#overlay').hide();
        $('#alert').hide();
        // 使用滚动条
        $('html').css('overflow','auto');
        $('body').css('overflow','auto');
    });

    // 让弹窗移动
    $('#alert h2').mousedown(function(ev){
        // 当按下鼠标时，获取当前点击的位置距离元素左边和顶部的距离
        var distanceX = ev.offsetX;
        var distanceY = ev.offsetY;
        $(document).mousemove(function(ev){
            // console.dir(ev);
            // 不管点击h2的哪个位置，始终获取h2左上角相对于整个文档的偏移量
            var left = ev.pageX - distanceX;
            var top = ev.pageY - distanceY;

            // 左右不能越界
            if(left < 0){
                left = 0;
            }   
            if(left <= $(document).scrollLeft()){
                left = $(document).scrollLeft();
            }
            if(left >= $(window).width()-$('#alert').width()+$(document).scrollLeft()){
                left  =$(window).width()-$('#alert').width()+$(document).scrollLeft();
            }

            // 上下不能越界
            if(top < 0){
                top = 0;
            }
            if(top <= $(document).scrollTop()){
                top = $(document).scrollTop();
            }
            if(top >= $(window).height()-$('#alert').height()+$(document).scrollTop()){
                top = $(window).height()-$('#alert').height()+$(document).scrollTop();
            }
            $('#alert').css({left:left,top:top});
        });
    });

    // 松开鼠标，取消移动
    $(document).mouseup(function(){
        $(this).off('mousemove');
    });
})
</script>